<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page isELIgnored="true" %>
<html>
<head>
    <title>部门管理</title>
    <%@include file="/comm/head.jsp"%>
</head>
<body>
<div id="root">
    <Collapse value="search">
        <Panel name="search">
            条件查询
            <p slot="content">
                <i-form inline :label-width="90">
                    <form-item label="名称:">
                        <i-input type="text" v-model="orgVo.orgName"/>
                    </form-item>
                    <form-item label="电话:">
                        <i-input type="text" v-model="orgVo.orgPhone"/>
                    </form-item>
                    <i-button type="primary"icon="ios-search" style="margin-left:20px" @click="pageNo=1;searchOrgList();">搜索</i-button>
                    <i-button type="success"icon="md-sync" @click="orgVo={};searchOrgList();pageNo=1"></i-button>
                </i-form>
            </p>
        </Panel>
    </Collapse>

    <card>
        <shiro:hasPermission name="bumen:add">
            <i-button type="success" @click="toAdd"icon="md-add">添加部门</i-button>
        </shiro:hasPermission>
    </card>
    <i-table :columns="myColumns" :data="pageResult.rows" :height="300" border stripe @on-selection-change="tableSelection=arguments[0]">
        <template slot-scope="{row,index}"  slot="action">
            <shiro:hasPermission name="bumen:update">
                <i-button type="warning" @click="toUpdate(row)">修 改</i-button>
            </shiro:hasPermission>
            <shiro:hasPermission name="bumen:del">
                <i-button type="error" @click="del(row,index)">删 除</i-button>
            </shiro:hasPermission>
        </template>
    </i-table>

    <modal v-model="updateFlag" title="修改部门" @on-ok="update">
        <i-form :label-width="80" style="padding: 30px" >
            <form-item label="部门名称">
                <i-input v-model="org.orgName" size="large"/>
            </form-item>
            <form-item label="部门简介">
                <i-input v-model="org.orgDescr" size="large"/>
            </form-item>
            <form-item label="联系电话">
                <i-input v-model="org.orgPhone" size="large"/>
            </form-item>
        </i-form>
    </modal>
    <modal v-model="addFlag" title="添加部门" >
        <i-form ref="org" :model="org"   :label-width="80" style="padding: 30px" :rules="ruleValidate">
            <form-item label="部门名称" prop="orgName">
                <i-input v-model="org.orgName" size="large"/>
            </form-item>
            <form-item label="部门简介" prop="orgDescr">
                <i-input v-model="org.orgDescr" size="large"/>
            </form-item>
            <form-item label="联系电话" prop="orgPhone">
                <i-input v-model="org.orgPhone" size="large"/>
            </form-item>
            <i-button style="margin-left:300px ;bottom: 12px;" type="primary" size="large" @click="add('org')">提交</i-button>
            <i-button   type="error" size="large" @click="cancel('org')" >取消</i-button>
        </i-form>
        <div slot="footer" >
        </div>
    </modal>
    <Page :total="pageResult.total"
          :page-size="pageSize"
    <%-- :current.sync="pageNo"--%>
          @on-change="pageNo=arguments[0];searchOrgList();"
    <%-- @on-change="searchUserList"--%>
          show-sizer
          :page-size-opts="[5,10,15]"
    <%-- @on-page-size-change="changPageSize"--%>
          @on-page-size-change="pageSize=arguments[0];searchOrgList();"
    />
</div>
<script>
    new Vue({
        el:"#root",
        data:{
            myColumns:[
               // {type:"selection",width:60,align:"center"},
                {title:"部门编号",key: "id",align:"center"},
                {title:"部门名称",key: "orgName",align:"center"},
                {title:"部门简介",key: "orgDescr",align:"center"},
                {title:"联系电话",key: "orgPhone",align:"center"},
                {title:"操作",slot:"action",align:"center"},
            ],
            pageResult:{
                rows:[],
                total:50,
            },
            myData:[],
            org:{},
            orgVo:{},
            pageNo:1,
            pageSize:5,
            tableSelection:[],
            updateFlag:false,
            addFlag:false,
            ruleValidate: {
                orgName: [
                    {required: true, message: '请输入部门名称', trigger: 'blur'}
                ],
                orgDescr: [
                    {required: true, message: '请输入部门简介', trigger: 'blur'}
                ],
                orgPhone: [
                    {required: true, message: '请输入部门电话', trigger: 'blur'},
                    { type: 'number', message: '请输入正确的电话号码', trigger: 'blur',transform:(value) => Number(value)}
                ],
            },

         },
        mounted(){
            this.searchOrgList();
        },
        methods:{
            searchOrgList(){
                axios.get(`${ctx}/renshi/bumen/search/${this.pageNo}/${this.pageSize}`,{params:this.orgVo})
                    .then(({data})=>{
                        this.pageResult=data.result;
                    });
            },
        /*    search(){
                axios.get(`${ctx}/renshi/bumen/list`)
                    .then(({data})=>{
                        this.myData=data.result;
                    });
            },*/
            toUpdate(row){
                //this.role=row;
                Object.assign(this.org,row);
                this.updateFlag=true;
            },
            update(){
                if(this.org.orgName=="") {
                    return this.$Message.error('修改失败!部门名称不能为空');
                }
                if(this.org.orgDescr=="") {
                    return this.$Message.error('修改失败!部门简介不能为空');
                }
                if(this.org.orgPhone.length!=11) {
                    return this.$Message.error('修改失败!电话号码必须为11位');
                }
                axios.post(`${ctx}/renshi/bumen/update`,this.org)
                    .then(({data})=>{
                        this.updateFlag=false;//关闭模态框
                        iview.Message.success({content:data.msg});
                        this.searchOrgList();
                    });
            },
            del(row,index){
                let _this=this;
                let flag= iview.Modal.confirm({
                    title:"您确实要删除么？",
                    content:"该操作不可逆，请谨慎操作。",
                    onOk(){
                        axios.get(`${ctx}/renshi/bumen/del?id=${row.id}`)
                            .then(({data})=>{
                                iview.Message.success({content:data.msg});
                                //_this.search();
                                _this.pageResult.rows.splice(index,1);
                            });
                    }
                });
            },
            toAdd(){
                this.org = {};
                this.addFlag=true;
            },
            add(name){
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        if(this.org.orgPhone.length!=11) {
                            return this.$Message.error('添加失败!电话号码必须为11位');
                        }
                axios.post(`${ctx}/renshi/bumen/add`,this.org)
                    .then(({data})=>{
                        this.addFlag=false;//关闭模态框
                        iview.Message.success({content:data.msg});
                        this.searchOrgList();

                    });
                    }else {
                        this.$Message.error('添加失败!');
                    }
                })
            },
            cancel(name){
                this.$refs[name].resetFields();
                this.addFlag=false;
            },
        }

    })
</script>
</body>
</html>
